//
//  SecondViewController.m
//  A段
//
//  Created by 月月 on 15/4/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SecondViewController.h"
#import "SecondTableViewCell.h"
#import "ListViewController.h"
#import "MyConnection.h"
#import "ContentViewController.h"
#import "WatchContentViewController.h"
@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *tableArray;
@property (nonatomic, retain)UITableView *table;

@end

@implementation SecondViewController
- (void)dealloc
{
    [_table release];
    [_tableArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"watch";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    [self createTable];
    [self startConnect];
}

- (void)leftButtonAction:(id)sender
{
    ListViewController *list = [[[ListViewController alloc] init]autorelease];
    [self.navigationController pushViewController:list animated:YES];
}

#warning 创建tableview
- (void)createTable
{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.height - 49) style:UITableViewStylePlain];
    [_table headerViewForSection:1];
//    _table.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)]autorelease];

    //行高
    [_table setRowHeight:150];
    _table.sectionHeaderHeight = 300;
    _table.delegate = self;
    _table.dataSource = self;
    //注册重用池
    [_table registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table];
    [_table release];
}
//tableviewheader方法
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    _table.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)]autorelease];
//    return _table.tableHeaderView;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 70;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:[dic objectForKey:@"cover_thumb_hd"]]];
    [cell.watchPhoto setImage:[UIImage imageWithData:dataImage]];
    cell.titleLabel.text = [dic objectForKey:@"title"];
    cell.pubdateLabel.text = [dic objectForKey:@"pubdate"];
    cell.summaryLabel.text = [dic objectForKey:@"summary"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [_table deselectRowAtIndexPath:indexPath animated:YES];
    
    //数据解析成字典
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    //字典取链接
    NSString *webStr = [dic objectForKey:@"link_share"];
    WatchContentViewController *watchController = [[WatchContentViewController alloc] init];
    //属性传值
    watchController.webString = webStr;
    [self.navigationController pushViewController:watchController animated:YES];
    
}

- (void)startConnect
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"iphone" forKey:@"ver"];
    [dic setObject:@"9" forKey:@"app_ver"];

    //网络请求
    __block SecondViewController *my = self;
    [MyConnection connectWithUrl:@"http://watch-cdn.idailywatch.com/api/list/news/zh-hans" parmaters:dic finishBlock:^(NSData *data) {
        
    //解析数据
    NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    [my.tableArray addObjectsFromArray:arr];
    [_table reloadData];
    }];
}
@end
